<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\WorkerMysqlConnect;

class user extends Model
{
    use HasFactory;
    use WorkerMysqlConnect;

    protected $table = 'user';

    protected $primaryKey = 'id';

    /**
     * 获取所有用户
     * @param int $startPage
     * @param int $limit
     * @return array
     */
    public static function getAllUser($startPage = 1,$limit = 10)
    {
        $table = self::query();
        if($limit && $startPage) $table = $table->offset(($startPage-1)*$limit)->limit($limit);
        return $table->get()->toArray();
    }

    /**
     * 根据用户id获取用户信息
     * @param $id
     * @param string $select
     * @return array
     */
    public static function getUserById($id,$select = '*')
    {
        return toArray(self::query()->select(explode(',',$select))->find($id));
    }

    /**
     * 根据账号密码获取用户信息
     * @param $account
     * @param $password
     * @return array
     */
    public static function getUserByAccountAndPassword($account,$password)
    {
        return toArray(self::query()->where(compact('account','password'))->first());
    }

    /**
     * 根据用户id更换用户头像id
     * @param $id
     * @param $portrait
     * @return int
     */
    public static function updateUserPortraitById($id,$portrait)
    {
        return self::query()->where(compact('id'))->update(compact('portrait'));
    }

    /**
     * 根据用户id更换用户名称
     * @param $id
     * @param $name
     * @return int
     */
    public static function updateUserNameById($id,$name)
    {
        return self::query()->where(compact('id'))->update(compact('name'));
    }

    /**
     * 根据用户id更换用户密码
     * @param $id
     * @param $password
     * @return int
     */
    public static function updateUserPasswordById($id,$password)
    {
        return self::query()->where(compact('id'))->update(compact('password'));
    }

    /**
     * 根据用户id更换用户信息
     * @param $id
     * @param array $data
     * @return int
     */
    public static function updateUserInformationById($id,$data = [])
    {
        $sql = createSelectSql('information_schema.COLUMNS','COLUMN_NAME',['table_name'=>self::table()]);
        $allColumn = array_column(self::connectWorker()->query($sql),'COLUMN_NAME','COLUMN_NAME');
        return self::connectWorker()->update(self::table())->cols(arrayIntersect($allColumn,$data,1))->where("id={$id}")->query();
    }

    /**
     * 根据id更新用户金额
     * @param $id
     * @param $money
     * @return int
     */
    public static function updateUserMoneyById($id,$money)
    {
        return self::query()->where(compact('id'))->update(compact('money'));
    }

    /**
     * 根据id更新用户金额和流水
     * @param $id
     * @param $money
     * @param int $all_shui
     * @return int
     */
    public static function updateUserMoneyAndAllshuiById($id,$money,$all_shui=0)
    {
        return self::query()->where(compact('id'))->update(compact('money','all_shui'));
    }
}
